﻿// Jinx AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System.Collections.Generic;
using System.Linq;
using Formality.Game.Database;
using System.Text.RegularExpressions;

namespace Formality.Game
{
    public class DurablePlayer<T> : IPlayer<T>
    {
        public const string DEFAULT_PROBLEM = "*2.01.";

        public DurablePlayer(string Name)
        {
            this.Name = Name;

            "insert or ignore into players (name, problem) values (@name, @problem)"
                .Execute(new {name = Name, problem = DEFAULT_PROBLEM});
        }

        #region IPlayer<T> Members

        public string ProblemId
        {
            get
            {
                return "select Problem from players where name=@name"
                    .ExecuteScalar<string>(new {name = Name});
            }
        }

        public string Name { get; private set; }

        public IEnumerable<string> CurrentProblemSubmissions
        {
            get
            {
                var Query =
                    @"  select proposition 
                        from players p join submissions s on p.playerid=s.playerid 
                        where s.problemid=p.problem and p.name=@Name";

                return Query.ExecuteReader(new {Name}, r => r[0].ToString());
            }
        }

        public IEnumerable<string> ProblemSubmissions(string Problem)
        {
            var Query =
                @"  select proposition 
                    from players p join submissions s on p.playerid=s.playerid 
                    where s.problemid=@Problem and p.name=@Name";

            return Query.ExecuteReader(new {Name, Problem}, r => r[0].ToString());
        }

        public void AddSubmission(Sentence<T> Submission, int Order)
        {
            var Query =
                @"  insert into submissions (PlayerId, ProblemId, SubmissionOrder, Proposition)
                    select PlayerId, Problem, @Order, @Item from Players";

            Query.Execute(new {Order, Submission.Item});
        }

        public void ChangeGoal(string NewProblemId)
        {
            "update Players set Problem=@NewProblemId where Name=@Name"
                .Execute(new {Name, NewProblemId});
        }

        #endregion

        public string LastProblemAttempted
        {
            get
            {
                var AllProblemsIds =
                    "select problemid from submissions s join players p on s.playerid=p.playerid where p.name=@Name"
                        .ExecuteReader(new {Name}, r => r[0].ToString().Trim('.','*')).ToList();

                var Level = AllProblemsIds.Max(p => (decimal?)decimal.Parse(p));

                return string.Format("*{0}.", Level);
            }
        }
    }
}